﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TPPool : SingletonMono<TPPool>
{
	public int maxPool;
	public GameObject prefab;

	Queue<GameObject> queen = new Queue<GameObject>();

	void Start()
	{
		CreatePool (maxPool);
	}

	void CreatePool(int max)
	{
		if (max < 0)
			return;
		if (max > 100)
			return;

		for (int i = 0; i < maxPool; i++) {
			GameObject go = GameObject.Instantiate (prefab, Vector3.zero, Quaternion.identity);
			go.transform.parent = transform;
			queen.Enqueue (go);
		}
	}

	public GameObject GetWater()
	{
		if (queen.Count > 0) {
			return queen.Dequeue ();
		} else {
			GameObject go = GameObject.Instantiate (prefab, Vector3.zero, Quaternion.identity);
			go.transform.parent = transform;
			queen.Enqueue (go);
		}

		return queen.Dequeue ();
	}

	public void RecoverWater(GameObject go)
	{
		Debug.Log ("1");
		//go.GetComponent<Rigidbody> ().useGravity = false;
		Destroy(go.GetComponent<Rigidbody>());
		queen.Enqueue (go);
		go.transform.parent = transform;
		go.transform.localPosition = Vector3.zero;
	}
}
